/**
 * @(#)BaseSmsServiceImpl.java 2016年11月11日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.domian.DMClientInfo;
import org.jsbd.boss.domian.TaskVo;
import org.jsbd.boss.domian.channel.ApkLoginTypeConfig;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.enums.LoginTypeEnum;
import org.jsbd.boss.enums.NetworkTypeEnum;
import org.jsbd.boss.enums.TaskStatusEnum;
import org.jsbd.boss.service.IClientLoginService;
import org.jsbd.boss.service.INewTaskService;
import org.jsbd.boss.util.EncUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 短信分发
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ClientLoginServiceImpl implements IClientLoginService {
	public static final Logger LOGGER = LoggerFactory.getLogger(ClientLoginServiceImpl.class);

	@Resource(name = "newTaskService")
	private INewTaskService newTaskService;

	/**
	 * 
	 */
	@Override
	public String getLoginCode(ChannelApkConfig apkConfig, DMClientInfo clientInfo) throws Exception {
		Integer smsFilterId = Integer.parseInt(apkConfig.getSmsId());
		switch (smsFilterId) {
		case 1:// 中移统一认证 SmsTypeEnum.SDK.getType()
			return 0 + EncUtil.toMd5(clientInfo.getSystime() + clientInfo.getAndroidId()) + "[中移统一认证]";
		case 2:// 咪咕一键登录 SmsTypeEnum.MIGU.getType()
			return 0 + EncUtil.toMd5(clientInfo.getSystime() + clientInfo.getAndroidId()) + "[咪咕一键登录]";
		case 3:// sdk imsi
				return 0 + EncUtil.toMd5(clientInfo.getImsi()) + "[中移统一认证]";
		case 4:// 咪咕 imsi
				return 0 + EncUtil.toMd5(clientInfo.getImsi()) + "[咪咕一键登录]";
		default:
			throw new Exception("not support smstype:" + smsFilterId);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IClientLoginService#sendSms(org.jsbd.boss.domian
	 * .TaskVo, org.jsbd.boss.domian.channel.ApkLoginTypeConfig)
	 */
	@Override
	public TaskStatusEnum sendSms(TaskVo taskVo, ApkLoginTypeConfig loginConfig) throws Exception {
		int loginType = loginConfig.getLoginType();
		TaskStatusEnum status = TaskStatusEnum.FAIL;
		if (loginType == LoginTypeEnum.PROXY.getType()) {
			taskVo.setNetworkType(NetworkTypeEnum.WIFI.getType());
		} else if (loginType == LoginTypeEnum.SDK.getType()) {
			status = TaskStatusEnum.SUCCESS;
		}
		taskVo.setLoginType(loginType);
		if (loginType != LoginTypeEnum.SDK.getType()) {
			taskVo.setPayMax(loginConfig.getPayMax());
			taskVo.setPayMin(loginConfig.getPayMin());
			taskVo.setPayRate(loginConfig.getPayRate());
			String mobile = loginConfig.getMobile();
			if (StringUtils.isNotBlank(mobile)) {
				taskVo.setMobile(mobile);
			}
			status = newTaskService.addTask(taskVo);
		}

		return status;
	}

}
